Page Replacement in OS
Page replacement is a memory-management technique used by an operating system in a virtual memory system. It determines which page currently in main memory (RAM) should be removed when a new page needs to be loaded but there is no free frame available.
A process may require more memory than the available physical RAM. The operating system therefore divides the process into fixed-size pages and RAM into fixed-size frames.
When a required page is not currently in RAM, a page fault occurs.Example Suppose RAM has 3 frames, and the following page-reference string is given: 1, 2, 3, 4 Initially:
| Frame | Page |
|---|---|
| F1 | 1 |
| F2 | 2 |
| F3 | 3 |
Now the CPU requests page 4. There is no free frame, so the operating system must remove one of pages 1, 2, or 3 and put page 4 in its place. Which page should be removed?
That depends on the page replacement algorithm being used.
The page that entered memory first is removed first. For example:
1 -> 2 -> 3
If page 4 must be loaded, page 1 is replaced because it entered memory first.The operating system replaces the page that will not be used for the longest period of time in the future.For example, if the pages currently in memory are:
1, 2, 3
and the future references are:2, 1, 4, 2, 3
Page 3 would be replaced because it is needed farthest in the future.LRU replaces the page that has not been used for the longest time in the past. For example: If pages have recently been accessed in this order:
2 -> 1 -> 3
then page 2 is the oldest among these three accesses. If a replacement is needed, LRU may select page 2.This is an improvement over FIFO. Each page has a reference bit. A page that has been recently used gets a second chance instead of being immediately replaced. It is commonly implemented using a circular queue (clock).
A system has 3 page frames and the following reference string:
7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2
Find the number of page faults and page hits using:
| Reference | Frame 1 | Frame 2 | Frame 3 | Result |
|---|---|---|---|---|
| 7 | 7 | - | - | Fault |
| 0 | 7 | 0 | - | Fault |
| 1 | 7 | 0 | 1 | Fault |
| 2 | 2 | 0 | 1 | Fault |
| 0 | 2 | 0 | 1 | Hit |
| 3 | 2 | 3 | 1 | Fault |
| 0 | 2 | 3 | 0 | Fault |
| 4 | 4 | 3 | 0 | Fault |
| 2 | 4 | 2 | 0 | Fault |
| 3 | 4 | 2 | 3 | Fault |
| 0 | 0 | 2 | 3 | Fault |
| 3 | 0 | 2 | 3 | Hit |
| 2 | 0 | 2 | 3 | Hit |
LRU replaces the page that has not been used for the longest period of time.
Reference string:7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2
| Reference | Frame 1 | Frame 2 | Frame 3 | Result |
|---|---|---|---|---|
| 7 | 7 | - | - | Fault |
| 0 | 7 | 0 | - | Fault |
| 1 | 7 | 0 | 1 | Fault |
| 2 | 2 | 0 | 1 | Fault |
| 0 | 2 | 0 | 1 | Hit |
| 3 | 2 | 0 | 3 | Fault |
| 0 | 2 | 0 | 3 | Hit |
| 4 | 4 | 0 | 3 | Fault |
| 2 | 4 | 0 | 2 | Fault |
| 3 | 4 | 0 | 3 | Hit |
| 0 | 4 | 0 | 3 | Hit |
| 3 | 4 | 0 | 3 | Hit |
| 2 | 2 | 0 | 3 | Fault |
Reference string:
7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2
| Reference | Frame 1 | Frame 2 | Frame 3 | Result |
|---|---|---|---|---|
| 7 | 7 | - | - | Fault |
| 0 | 7 | 0 | - | Fault |
| 1 | 7 | 0 | 1 | Fault |
| 2 | 2 | 0 | 1 | Fault |
| 0 | 2 | 0 | 1 | Hit |
| 3 | 2 | 0 | 3 | Fault |
| 0 | 2 | 0 | 3 | Hit |
| 4 | 2 | 4 | 3 | Fault |
| 2 | 2 | 4 | 3 | Hit |
| 3 | 2 | 4 | 3 | Hit |
| 0 | 0 | 4 | 3 | Fault |
| 3 | 0 | 4 | 3 | Hit |
| 2 | 0 | 2 | 3 | Fault |